<?php
class ApiController extends CController
{
    public function init()
    {
        $request = Yii::app()->request;

        // check post
        if(!$request->isPostRequest)
        {
            throw new CHttpException(403, 'Forbidden');
        }

        // validate secret key
        $secretKey = $request->getParam('secret_key');
        if($secretKey != CONST_API_SECRET_KEY)
        {
            throw new CHttpException(401, 'Unauthorized');
        }
    }

    public function actionPing()
    {
        // param: user_id
        $request = Yii::app()->request;
        $userId = $request->getParam('user_id');
        if($userId === null)
        {
            throw new CHttpException(400, 'Bad Request: user_id empty');
        }

        // param: platform_id
        $platformId = $request->getParam('platform_id');
        if($platformId === null)
        {
            throw new CHttpException(400, 'Bad Request: platform_id empty');
        }

        // add user online
        UserUtility::ping($userId, $platformId);
    }
}
